/***
Welcome to Blackjack!
### Dealer's Hand
? of ?
Ten of Clubs

### Your Hand (13)
Seven of Spades
Six of Diamonds

(H)it or (S)tand? h
### Dealer's Hand
? of ?
Ten of Clubs

### Your Hand (20)
Seven of Spades
Six of Diamonds
Seven of Clubs

(H)it or (S)tand? s
### Dealer's Hand(20)
Ten of Diamonds
Ten of Clubs

### Your Hand (20)
Seven of Spades
Six of Diamonds
Seven of Clubs

PUSH - YOU LOSE! (Welcome to Vegas, baby!) >:-]
***/

package blackjack;

import blackjack.PlayingCard.Rank;
import blackjack.PlayingCard.Suit;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) {

        System.out.println("Welcome to Blackjack!");

        Deck deck = new StandardDeck();
        Hand playerHand = new StandardHand();
        Hand dealerHand = new StandardHand();

        // initialize the deck with all 52 cards
        for(Rank rank : Rank.values()) {
            for(Suit suit : Suit.values()) {
                deck.addCard(new PlayingCard(rank, suit));
            }
        }

        // ensure that the deck is sufficiently randomized
        deck.shuffle();

        // deal two cards to each hand
        playerHand.addCard(deck.deal());
        dealerHand.addCard(deck.deal());
        playerHand.addCard(deck.deal());
        dealerHand.addCard(deck.deal());

        // show the current game state to the player
        System.out.println("### Dealer's Hand");
        System.out.println(dealerHand.asObscuredString());
        System.out.println("### Your Hand ("+playerHand.getValue()+")");
        System.out.println(playerHand.asString());

        // allow the user to stand or hit until bust or 21
        while(playerHand.getValue() < 21) {
            String response = queryHitOrStand();
            if(response.equals("h")) {
                playerHand.addCard(deck.deal());
                System.out.println("### Dealer's Hand");
                System.out.println(dealerHand.asObscuredString());
                System.out.println("### Your Hand ("+playerHand.getValue()+")");
                System.out.println(playerHand.asString());
            } else {
                break;
            }
        }

        // if the player has exceeded 21, taunt them and end
        if(playerHand.getValue() > 21) {
            System.out.println("YOU BUSTED!! :-(");
            return;
        }

        // allow the dealer to hit or stand until bust or 21
        while(dealerHand.getValue() < 16) {
            dealerHand.addCard(deck.deal());
        }
        
        System.out.println("### Dealer's Hand("+dealerHand.getValue()+")");
        System.out.println(dealerHand.asString());
        System.out.println("### Your Hand ("+playerHand.getValue()+")");
        System.out.println(playerHand.asString());

        // if the dealer has exceeded 21, congratulate the player
        if(dealerHand.getValue() > 21) {
            System.out.println("DEALER BUSTED!! :-)");
            return;
        }

        // if the dealer's hand is more valuable than the player's hand, taunt the player
        if(playerHand.getValue() < dealerHand.getValue()) {
            System.out.println("DEALER WINS! :-P");
        } else if (playerHand.getValue() > dealerHand.getValue()) {
            System.out.println("YOU WIN! :-O");
        } else {
            System.out.println("PUSH - YOU LOSE! (Welcome to Vegas, baby!) >:-]");
        }
    }

    private static String queryHitOrStand() {
        String response = "";
        while(!response.toLowerCase().equals("h") && !response.toLowerCase().equals("s")) {
            System.out.print("(H)it or (S)tand? ");
            System.out.flush();
            try {
                response = new BufferedReader(new InputStreamReader(System.in)).readLine();
            } catch (IOException e) {
                response = "s";
            }
        }
        return response;
    }

}
